﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taurus.Platform.DTO;
using Taurus.Platform.Net;
using HtmlAgilityPack;
using Newtonsoft.Json.Linq;
using Taurus.Platform.XML;

namespace Taurus.Platform.BLL
{
    public class FriendBll
    {
        private const string FRIEND_START_URL = "http://www.kaixin001.com/friend/index.php?uid={1}&startype=0&viewtype=&start={0}";
        private const string FRIEND_GROUP_URL = "http://www.kaixin001.com/friend/editfriend_dialog.php?fuid={0}";

        private UserAccount _myAccount;
        private IPersistence<UserAccount> _per;


        public FriendBll(UserAccount myAccount)
        {
            _myAccount = myAccount;
            _per = new UserAccountPersistence();
        }

        public List<UserAccount> GetMyFriendsFromNet() 
        {
            HtmlDocument doc = Util.AccessPage(_myAccount, string.Format(FRIEND_START_URL, 0, _myAccount.UID));
            int pageCount = Util.GetPageCount(doc, 40);

            List<UserAccount> list = GetFriendsByHtml(doc);

            for (int i = 1; i < pageCount; i++)
            {
                doc = Util.AccessPage(_myAccount, string.Format(FRIEND_START_URL, i * 40, _myAccount.UID));
                list.AddRange(GetFriendsByHtml(doc));
            }

            var uids = from ua in list select ua.UID;
            _myAccount.Friends = uids.ToList();
           
            return list;
        }

        public List<UserAccount> GetMyFriendsFromConfig()
        {
            List<UserAccount> list = new List<UserAccount>();
            foreach (var uid in _myAccount.Friends)
            {
                list.Add(_per.GetObject(uid));
            }
            return list;
        }

        public void InitFriendGroup(UserAccount friend)
        {
            //TODO:初始化用户组
            //HtmlDocument doc = Util.AccessPage(_myAccount, string.Format(FRIEND_GROUP_URL, friend.UID));
            //friend.Groups = new List<string>();
            ////HtmlWeb hw = new HtmlWeb();
            ////HtmlDocument doc = hw.Load(@"e:\Noname1.html");
            //string temp = doc.DocumentNode.SelectNodes("//script")[14].InnerText;
            //int s = temp.IndexOf("[");
            //int e = temp.IndexOf(";");
            //string groupdata = temp.Substring(s, e - s);
            //JArray ja = JArray.Parse(groupdata);
            //foreach (var item in ja)
            //{
            //    if ((string)item["checked"] == "1")
            //    {
            //        friend.Groups.Add((string)item["name"]);
            //    }
            //}
        }

        private List<UserAccount> GetFriendsByHtml(HtmlDocument doc)
        {
            List<UserAccount> list = new List<UserAccount>();
            HtmlNode node = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/div[8]");
            foreach (var fnode in node.ChildNodes)
            {
                if (fnode.OriginalName == "div" && fnode.ChildNodes.Count==7)
                {
                    UserAccount friend = new UserAccount();
                    
                    friend.Name = fnode.SelectNodes(".//a")[1].InnerText;
                    friend.UID = Util.GetNumber(fnode.SelectNodes(".//a")[1].Attributes["href"].Value).ToString();
                    friend.Signature = fnode.SelectNodes(".//p")[1].Attributes["title"].Value;
                    friend.PicFullName = fnode.SelectNodes(".//img")[0].Attributes["src"].Value;

                    list.Add(friend);
                }
            }

            return list;
        }
    }
}
